{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 03 实现简单线性回归法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = np.array([1., 2., 3., 4., 5.])\n",
    "y = np.array([1., 3., 2., 3., 5.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADUpJREFUeJzt3FFoZOd5xvHniVZpJhsbXWSSerU2W0ORCUkTuYNLWAitk0YuMakIvYghuQgF3bTBIUGhCpTQ3oQiCDG0FITt1CZOTFjLSwmpFUNqHEO9jWRtKsdrlWAMXilhZYKwNwyJLL+90MjdtSXNGe85Onpn/j9YVnP20+47zOrP0TfnyBEhAEAe76h7AABAbwg3ACRDuAEgGcINAMkQbgBIhnADQDKFwm17xPYZ28/bvmD7o1UPBgDY27GC6+6R9FhE/JXtd0p6d4UzAQAO4G434Ni+XtLPJN0c3K0DALUrcsZ9s6QNSd+2/WFJS5LujojfXLnI9pSkKUk6fvz4H99yyy1lzwoAfWtpaenliGgWWVvkjLsl6WlJpyPinO17JL0SEX+/3+e0Wq1YXFzsZWYAGGi2lyKiVWRtkTcnL0q6GBHnOo/PSLr17Q4HALg2XcMdEb+S9JLtsc6hj0t6rtKpAAD7KnpVyRclPdS5ouQFSV+obiQAwEEKhTsizksqtPcCAKgWd04CQDKEGwCSIdwA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9GxAfrnqdstm+QdENEPGP7OklLkib76PWzpOMRcdn2sKSnJN0dEU/XPFppbH9ZUkvS9RFxZ93zlMn2i5JaEdGXN9/YfkDSTyLi3s7Ve++OiM391pd5xt3Xt8ZHxJOSfl33HFWJiF9GxDOdj1+VdEE7d832hdhxufNwuPOrb96Zt31S0qck3Vv3LOiN7eslfUzSfZIUEb87KNpSueHe69b4vvnCHyS2T0kal3Su3knK1dlKOC/pkqTHI6Kfnt+3JH1V0ut1D1KRkPQj20udH6/RT26WtCHp252trnttHz/oE8oMd6Fb43G02X6PpEckfSkiXql7njJFxHZEfEQ7d//eZrsvtrxs3ynpUkQs1T1LhU5HxK3a+Smlf9PZuuwXxyTdKulfI2Jc0m8kHfgeYZnh5tb45Dp7v49Ieigi5uuepyqdb0OfkHRHzaOU5bSkT3f2gR+WdLvt79Q7UrkiYr3z+yVJj2pna7ZfXJR08YrvAM9oJ+T7KjPc3BqfWOfNu/skXYiIb9Y9T9lsN22PdD5uSPqEpOfrnaocETETEScj4pR2vu5+HBGfq3ms0tg+3nnDXJ0thE9K6puruyLiV5Jesr370wE/LunAiwKK/HTAov/427k1Pg3b35P0p5Lea/uipK9HxH31TlWq05I+L2mlsw8sSV+LiB/WOFOZbpD0QOfqp3dI+n5E9N1lc33q/ZIe3Tm30DFJ342Ix+odqXRflPRQ56T3BUlfOGgxt7wDQDLcOQkAyRBuAEiGcANAMoQbAJIh3ACQDOEGgGQINwAk839HqqHJzIF2NgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "plt.axis([0, 6, 0, 6])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_mean = np.mean(x)\n",
    "y_mean = np.mean(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "num = 0.0\n",
    "d = 0.0\n",
    "for x_i, y_i in zip(x, y):\n",
    "    num += (x_i - x_mean) * (y_i - y_mean)\n",
    "    d += (x_i - x_mean) ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = num/d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b = y_mean - a * x_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_hat = a * x + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGEBJREFUeJzt3XmclWX9xvHPVxh1FHVKxwWQSLRB0hAdtx+Gu+NCSmWppZkbmUuKOSquASoQmrspIS6hkCKgWYJmmqKCzgA6iAwCLiwaoI0CDjgM398f9yGtgDkD58xz7nOu9+vly5njg17Hw1w+3s+9mLsjIiLx2CTpACIi0jwqbhGRyKi4RUQio+IWEYmMiltEJDIqbhGRyKRV3GZWYmajzWymmb1tZgdmO5iIiKxd6zSvuw0Y7+4nmtmmwBZZzCQiIuthTS3AMbOtgTeAXVyrdUREEpfOHfcuwGLgfjPrClQDF7n78q9eZGa9gd4AW2655T6dO3fOdFYRkbxVXV29xN1L07k2nTvucmAS0N3dJ5vZbcBn7n7Nun5NeXm5V1VVNSeziEhBM7Nqdy9P59p0Hk7OB+a7++TU96OBvTc0n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6sd1JAOrsDpvsP35Cl8dEws5HAIcB2ZjYfuM7d70s2VUZ1B04DalLjwABXuvtfE8yUSTsBD6ZmP20CPOrueTdtLk/tAIwN9xa0Bh5x9/HJRsq4C4GHUze9c4Ez1nexlryLiERGKydFRCKj4hYRiYyKW0QkMipuEZHIqLhFRCKj4hYRiYyKW0QkMv8PdDQutctGuqIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "plt.plot(x, y_hat, color='r')\n",
    "plt.axis([0, 6, 0, 6])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_predict = 6\n",
    "y_predict = a * x_predict + b\n",
    "y_predict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 封装我们自己的SimpleLinearRegression\n",
    "\n",
    "代码参见 [这里](playML/SimpleLinearRegression.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.2])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from playML.SimpleLinearRegression import SimpleLinearRegression1\n",
    "\n",
    "reg1 = SimpleLinearRegression1()\n",
    "reg1.fit(x, y)\n",
    "reg1.predict(np.array([x_predict]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg1.a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.39999999999999947"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg1.b_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_hat1 = reg1.predict(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGEBJREFUeJzt3XmclWX9xvHPVxh1FHVKxwWQSLRB0hAdtx+Gu+NCSmWppZkbmUuKOSquASoQmrspIS6hkCKgWYJmmqKCzgA6iAwCLiwaoI0CDjgM398f9yGtgDkD58xz7nOu9+vly5njg17Hw1w+3s+9mLsjIiLx2CTpACIi0jwqbhGRyKi4RUQio+IWEYmMiltEJDIqbhGRyKRV3GZWYmajzWymmb1tZgdmO5iIiKxd6zSvuw0Y7+4nmtmmwBZZzCQiIuthTS3AMbOtgTeAXVyrdUREEpfOHfcuwGLgfjPrClQDF7n78q9eZGa9gd4AW2655T6dO3fOdFYRkbxVXV29xN1L07k2nTvucmAS0N3dJ5vZbcBn7n7Nun5NeXm5V1VVNSeziEhBM7Nqdy9P59p0Hk7OB+a7++TU96OBvTc0nIiIbJwmi9vdPwLmmVlZ6qXDgRlZTSUiIuuU7qySC4GHUzNK5gJnZC+SiIisT1rF7e7TgLTGXkREJLu0clJEJDIqbhGRyKi4RUQio+IWEYmMiltEJDIqbhGRyKi4RUQio+IWEYmMiltEJDIqbhGRyKi4RUQio+IWEYmMiltEJDIqbhGRyKi4RUQio+IWEYmMiltEJDIqbhG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ASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "plt.plot(x, y_hat1, color='r')\n",
    "plt.axis([0, 6, 0, 6])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 向量化实现SimpleLinearRegression\n",
    "\n",
    "代码参见 [这里](playML/SimpleLinearRegression.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'SimpleLinearRegression2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-33721b61338e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mplayML\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSimpleLinearRegression\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mSimpleLinearRegression2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mreg2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSimpleLinearRegression2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mreg2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mreg2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mx_predict\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'SimpleLinearRegression2'"
     ]
    }
   ],
   "source": [
    "from playML.SimpleLinearRegression import SimpleLinearRegression2\n",
    "\n",
    "reg2 = SimpleLinearRegression2()\n",
    "reg2.fit(x, y)\n",
    "reg2.predict(np.array([x_predict]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg2.a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg2.b_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 向量化实现的性能测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = 1000000\n",
    "big_x = np.random.random(size=m)\n",
    "big_y = big_x * 2 + 3 + np.random.normal(size=m)\n",
    "%timeit reg1.fit(big_x, big_y)\n",
    "%timeit reg2.fit(big_x, big_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg1.a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg1.b_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg2.a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg2.b_"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
